package com.dule.Medic.beans;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.tapestry5.beaneditor.NonVisual;

import com.dule.Medic.model.Doctor;
import com.dule.Medic.model.MedEntry;
import com.dule.Medic.model.Patient;
import com.dule.Medic.model.Prescription;


@SuppressWarnings("serial")
@Entity
@Table(name="medEntry")
public class MedEntryBean implements MedEntry,Serializable
{
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@NonVisual
	private Long id;
	
	@ManyToOne(targetEntity=PatientBean.class) 
	private Patient patient;
	@ManyToOne(targetEntity=DoctorBean.class) 
	private Doctor doctor;
	
	@OneToMany(targetEntity=PrescriptionBean.class,cascade=CascadeType.ALL) 
	private List<Prescription> prescriptions =new LinkedList<Prescription>();
	
	@Basic
	private String date;
	@Basic
	private String data;
	
	public Patient getPatient() 
	{
		return this.patient;
	}
	public void setPatient(Patient patient) {
		this.patient = patient;
	}
	public Doctor getDoctor() {
		return doctor;
	}
	public void setDoctor(Doctor doctor) {
		this.doctor = doctor;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}
	
	public List<Prescription> getPrescriptions() {
		return prescriptions;
	}
	public void setPrescriptions(List<Prescription> prescriptions) {
		this.prescriptions = prescriptions;
	}
	
	public Prescription getPrescriptionById(Long id) 
	{
		for(Prescription pr:getPrescriptions())
        {
			if(pr.getId().equals(id))
                return pr;
        }
        return null;
    }
	public boolean UpdatePrescription(Prescription prescription) 
	{
		for(Prescription pr:prescriptions)
        {
			if(pr.getId().equals(prescription.getId()))
			{
				prescriptions.remove(pr);
				prescriptions.add(prescription);
				return true;
			}
        }
        return false;
    }
	
	public MedEntryBean() {
		// TODO Auto-generated constructor stub
	}
}
